package com.xiaoshuidi.cloud.module.finance.controller.admin.invoice;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.*;
import com.xiaoshuidi.cloud.module.finance.convert.finance.InvoiceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceApartmentDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceConfigDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceTaxRateDO;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "管理后台 - 发票")
@RestController
@RequestMapping("/finance/invoice/config")
public class FinanceInvoiceConfigController {
    @Resource
    private FinanceInvoiceConfigService financeInvoiceConfigService;

    @PostMapping("/create")
    @Operation(summary = "新增税号")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<Integer> create(@Valid @RequestBody FinanceInvoiceConfigCreateReqVO reqVO) {
        financeInvoiceConfigService.save(reqVO);
        return CommonResult.success();
    }

    @GetMapping("/page")
    @Operation(summary = "税号列表")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:query')")
    public CommonResult<PageResult<FinanceInvoiceConfigVO>> page(FinanceInvoiceConfigPageReqVO reqVO) {
        PageResult<FinanceInvoiceConfigDO> page = financeInvoiceConfigService.getPage(reqVO);
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToPage2(page));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "所有税号")
    public CommonResult<List<FinanceInvoiceConfigSimpleVO>> listAll() {
        List<FinanceInvoiceConfigDO> list = financeInvoiceConfigService.getAll();
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToList4(list));
    }

    @GetMapping("/get")
    @Operation(summary = "税号详情")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:query')")
    public CommonResult<FinanceInvoiceConfigVO> get(@RequestParam("id") Integer id) {
        FinanceInvoiceConfigDO configDO = financeInvoiceConfigService.getById(id);
        return CommonResult.success(InvoiceConvert.INSTANCE.convert(configDO));
    }

    @PostMapping("/switch")
    @Operation(summary = "启用开关")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<Void> updateSwitch(@RequestBody FinanceInvoiceConfigSwitchReqVO reqVO) {
        financeInvoiceConfigService.updateSwitch(reqVO);
        return CommonResult.success();
    }

    @PostMapping("/update")
    @Operation(summary = "修改税号")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<Void> update(@Valid @RequestBody FinanceInvoiceConfigUpdateReqVO reqVO) {
        financeInvoiceConfigService.update(reqVO);
        return CommonResult.success();
    }

    @PostMapping("/tax")
    @Operation(summary = "添加税率及费项")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<Void> addFeeTax(@Valid @RequestBody FinanceInvoiceFeeTaxReqVO reqVO) {
        financeInvoiceConfigService.updateTaxRate(reqVO);
        return CommonResult.success();
    }

    @GetMapping("/tax")
    @Operation(summary = "获取税率及费项列表")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<List<FinanceInvoiceTaxRateVO>> getFeeTax(@RequestParam("configId") Integer id) {
        List<FinanceInvoiceTaxRateDO> l = financeInvoiceConfigService.getFeeTax(id);
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToList2(l));
    }

    @PostMapping("/apartment")
    @Operation(summary = "关联房源")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<Void> apartment(@Valid @RequestBody FinanceInvoiceConfigApartmentReqVO reqVO) {
        financeInvoiceConfigService.updateApartment(reqVO);
        return CommonResult.success();
    }

    @GetMapping("/apartment")
    @Operation(summary = "获取关联房源列表")
    @PreAuthorize("@ss.hasPermission('finance:invoice:config:create')")
    public CommonResult<List<FinanceInvoiceConfigApartmentRespVO>> apartmentlist(@RequestParam("configId") Integer id) {
        List<FinanceInvoiceApartmentDO> l = financeInvoiceConfigService.getApartment(id);
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToList3(l));
    }
}
